package com.adxon.data;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.adxon.MsgException;

public class Session extends Asset{
	public final static String BSK="i7";

	protected String	pk;		// permalink

	public Session(){
		bsk=BSK;
	}
	@Override
	public void commit()throws MsgException{
		if(tm==0){
			if(pk==null||pk.length()<7)
				pk="initial";
			else{
				if(pk.length()==7)
					try{
						//String i=allocateId();
						MessageDigest m=MessageDigest.getInstance("MD5");
						//m.update(i.getBytes(),0,i.length());
						pk=new BigInteger(1,m.digest()).toString(16);
						//System.out.println(pk);
					}catch(NoSuchAlgorithmException e){
						e.printStackTrace();
					}
				super.commit();
			}
		}
	}
	public String getPermaLink(){
		return pk;
	}
	public void setPermaLink(String permaLink) {
		pk=permaLink;
	}
	@Override
	public void setup()throws MsgException{
		stp="id VARCHAR(64)NOT NULL COMMENT 'id',"+
			"ow BIGINT NOT NULL COMMENT 'owner id',"+
			"tc BIGINT NOT NULL COMMENT 'create time',"+
			"tm BIGINT NOT NULL COMMENT 'modify time',"+
			"PRIMARY KEY(id),"+
			"INDEX(tm)";
		super.setup();
	}
	@Override
	public void updateExt()throws MsgException{
		if(pk!=null){
			if(pk.length()>7)
				try{
					super.updateExt();
					//return this;
				}catch(MsgException e){}
			pk="initial";
		}
		//return this;
	}
}